package edu.biwu.request;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@WebServlet("/req/upload01")
@MultipartConfig
public class ReqUploadDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Part uploadFile = request.getPart("uploadFile");
        //System.out.println(uploadFile.getContentType());
        //System.out.println(uploadFile.getSize());
        //System.out.println(uploadFile.getSubmittedFileName());
        String fileName = uploadFile.getSubmittedFileName();

        String extension = fileName.substring(fileName.lastIndexOf("."));
        String newFilename=UUID.randomUUID()+ extension;

        File file = new File("D:/uploadFile");
        if (!file.exists()) {
            file.mkdirs();
        }

        uploadFile.write(file+"/"+newFilename);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(fileName+"上传成功");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
